﻿package com.hc.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.AdapterView;

/**
 * 3G网络管理
 * @ClassName: NetWorkManager
 * @Description: TODO
 * @date 2013-3-29 下午4:14:19
 * 
 */
public class NetWorkManager {

	private ConnectivityManager connectivityManager;
	private NetworkInfo networkInfo;
	private NetworkInfo[] networkInfos;
	private Method setMobileDataEnabledMethod;
	public NetWorkManager(Context context) {
		connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
//		networkInfos = connectivityManager.getAllNetworkInfo();
		networkInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
	}
	//操作gprs
	public void initOperate(boolean isOpen) {
		Class<?> conMgrClass = null; // ConnectivityManager
		Field iConMgrField = null; // ConnectivityManager类中的字
		Object iConMgr = null; // IConnectivityManager类的引用
		Class<?> iConMgrClass = null; // IConnectivityManager
		setMobileDataEnabledMethod = null; // setMobileDataEnabled方法
		try {
			// 取得ConnectivityManager
			conMgrClass = Class.forName(connectivityManager.getClass().getName());
			// 取得ConnectivityManager类中的对象mService
			iConMgrField = conMgrClass.getDeclaredField("mService");
			// 设置mService可访
			iConMgrField.setAccessible(true);
			// 取得mService的实例化类IConnectivityManager
			iConMgr = iConMgrField.get(connectivityManager);
			// 取得IConnectivityManager
			iConMgrClass = Class.forName(iConMgr.getClass().getName());
			// 取得IConnectivityManager类中的setMobileDataEnabled(boolean)方法
			setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod(
					"setMobileDataEnabled", Boolean.TYPE);
			// 设置setMobileDataEnabled方法可访
			setMobileDataEnabledMethod.setAccessible(true);
			setMobileDataEnabledMethod.invoke(iConMgr, isOpen);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
	/**
	 * 开启GPRS
	* @Title: openGPRS
	* @throws
	 */
	public void openGPRS() {
//		State state = networkInfo.getState();
		initOperate(true);
	}
    
	/**
	 * 关闭GPRS
	* @Title: closeGPRS
	* @Description: TODO    
	* @throws
	 */
	public void closeGPRS() {
		State state = networkInfo.getState();
//		if (State.CONNECTED == state || State.CONNECTING == state) {
//			Log.e("--ww--","---"+networkInfo.getExtraInfo());
//			connectivityManager
//					.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
//							networkInfo.getExtraInfo());
//		}
		initOperate(false);
	}
	
//	Handler hadle;
	/**
	 * ？？
	* @Title: isActive
	* @Description: TODO
	* @return    
	* @throws
	 */
	public boolean isActive() {
//		hadle = new Handler();
//		Message msg = new Message();
//		msg.setTarget(target)
//		hadle.sendMessage();
		networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		return networkInfo != null;
	}
    
	/**
	 * 是否连接
	* @Title: isConnect
	* @Description: TODO
	* @return    
	* @throws
	 */
	public boolean isConnect() {
		return networkInfo.isConnected();
	}
	
	/**
	 * 获取连接状态
	* @Title: getGPRSState
	* @Description: TODO
	* @return    
	* @throws
	 */
	public State getGPRSState() {
	    return networkInfo.getState();
	}
   
	/**
	 * 设置网络优先级
	* @Title: setNetworkPreference
	* @Description: TODO
	* @param preference    
	* @throws
	 */
	public void setNetworkPreference(int preference) {
		connectivityManager.setNetworkPreference(preference);
	}

	/**
	 * 设置apn
	 * 
	 * @Title: setAPN
	 * @Description: TODO
	 * @throws
	 */
	public void setAPN() {

	}

	// Log.e("通知", String.valueOf(networkInfo == null));
	// if (networkInfo == null) {
	// startActivity(new Intent(
	// android.provider.Settings.ACTION_WIFI_SETTINGS));
	// // startActivity(new
	// // Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
	// }
	// // 得到APN
	// Log.e("通知", networkInfo.getExtraInfo());
	// boolean available = networkInfo.isAvailable();
	// if (available) {
	// Log.e("通知", "当前的网络连接可);
	// } else {
	// Log.e("通知", "当前的网络连接);
	// }
	//
	// State state = connectivityManager.getNetworkInfo(
	// ConnectivityManager.TYPE_MOBILE).getState();
	// if (State.CONNECTED == state) {
	// Log.e("通知", "GPRS网络已连);
	// } else {
	// Log.e("通知", "GPRS没网络已连接");
	// }
	//
	// state = connectivityManager.getNetworkInfo(
	// ConnectivityManager.TYPE_WIFI).getState();
	// if (State.CONNECTED == state) {
	// Log.e("通知", "WIFI网络已连);
	// } else {
	// Log.e("通知", "wifi没网络已连接");
	// }

}
